#!/bin/sh
ss_run_time()
{ 


 ss_run_time=`cat /opt/fonsview/NE/ss/data/proc/mng/nodes 2> /dev/null | grep "*" | head -1 | awk '{print $11}'`


 if [ "$ss_run_time" = "" ];then
    echo "-1"


else
 echo $ss_run_time 

fi

}


ss_version()

{

 

 count=-1
 cat /opt/fonsview/NE/ss/data/proc/mng/version | while read line

   do

    if [ $count -eq -1 ];then
      count=$(($count+1)) 
      continue

    else

      version[$count]=`echo $line | awk '{print $3}'`      
      count=$(($count+1))
      echo ${count} 
      echo ${version[$conut]}
      echo ${version[0]}

    fi
   
  done

  echo ${version[0]}

  echo ${version[1]}



  

}

ha_alerm()
{

ss_type=`cat /opt/fonsview/NE/ss/data/proc/mng/nodes 2> /dev/null | grep "*"  | head -1 | awk '{print $2}'`

if [ "$ss_type" = "" ];then

 return 1

fi
scflag=`echo $ss_type | grep "SC"`



if [ "$scflag" != "" ];then
 HA_State=`cat /opt/fonsview/NE/ss/data/proc/hastate | grep "HA State" | awk -F ':' '{print $2}'`
 Peer_state=`cat /opt/fonsview/NE/ss/data/proc/hastate | grep  "Peer state" | awk -F ':' '{print $2}'`



  
 
 if [ $HA_State = "STANDBY" -a $Peer_state = "ACTIVE" ];then
     return 0
         
 elif [ $HA_State = "ACTIVE" -a $Peer_state = "STANDBY" ];then
     return 0
 else
     
     return 1    
 fi      
  
else
  return 0

fi

}




disk_alerm()
{


disk_string=`cat /opt/fonsview/NE/ss/data/proc/fsio/disks | tail -2 | head -1`



total_disk_string=`echo $disk_string | awk '{print $2}'`

valid_disk_string=`echo $disk_string | awk '{print $3}'`

total_disk_num=`echo $total_disk_string | awk -F '=' '{print $2}'`

valid_disk_num=`echo $valid_disk_string | awk -F '=' '{print $2}'`



if [ $total_disk_num -eq $total_disk_num ];then
   return 0
 else
    return 1
fi
}

system_disk_alerm()

{
  
  resultstring=`cat /proc/mdstat | grep -c "\[UU\]"`
  echo $resultstring

}

RRS_connect()

{

  ha=`cat /opt/fonsview/NE/rrs/proc/app | grep "HA MODE" | awk -F ':' '{print $2}'`

  if [ $ha != "ACTIVE" ];then
   echo 0;
   exit 0;

  fi


  if [ -f /opt/fonsview/NE/rrs/check.log ];then

    ESTAB_num=`tail -n 30  /opt/fonsview/NE/rrs/check.log | grep "ESTAB" | awk '{print $1}'`

    ACK_num=`tail -n 30  /opt/fonsview/NE/rrs/check.log | grep "LAST-ACK" | awk '{print $1}'`

    else
      
    ESTAB_num=`tail -n 30 /opt/fonsview/NE/rrs/log/monitor.log | grep "ESTAB" | awk '{print $1}'`

    ACK_num=`tail -n 30 /opt/fonsview/NE/rrs/log/monitor.log |  grep "LAST-ACK" | awk '{print $1}'`
  fi

 if [ $1 = "ACK" ];then
   if [ "$ACK_num"X = "X" ]; then
      ACK_num=0
   fi
   echo $ACK_num

 elif [ $1 = "ESTAB" ];then
  echo $ESTAB_num
  
fi 
  

}




cmm_status()

{

  if [ $1 -eq 1 ];then
     statu=`ps -ef | grep -v grep | grep -c "/opt/fonsview/NE/cmm/cmm.jar"`
     if [ $statu -gt 0 ];then
      echo 1
     else
      echo -2   
     fi

  elif [ $1 -eq 2 ];then

   cmm_flag=`ps -ef | grep -v grep | grep -c '/opt/fonsview/NE/cmm/cmm.jar'`

   if [ $cmm_flag -eq 0 ];then
      ucarp=`ps -ef | grep -v grep |grep -c "ucarp"`
     if [ $ucarp -gt 0 ];then
      echo 0
     else
      echo -1
     fi
   else
   vip=`ps -ef | grep ucarp | grep "/opt/fonsview/bin/ucarp" | awk '{print $17}'`
   ip=($vip)
      if [ ${ip[0]} != "cmm_pass" ];then
        vip=${ip[0]};
      else
        vip=${ip[1]}
      fi

   real_ip=`ip a | grep $vip`
      if [ "$real_ip" = "" ];then
        echo 0
      else
        echo 1
      fi
    fi
fi

}

Functiontype=$1

if [ "$Functiontype" = "runtime" ];then
    ss_run_time
elif [ "$Functiontype" = "ha" ];then
    ha_alerm
    echo $?
elif [ "$Functiontype" = "disk" ];then
    disk_alerm 
    echo $?
elif [ "$Functiontype" = "system_disk" ];then
    system_disk_alerm
elif [ "$Functiontype" = "RRS_connect" ];then
    RRS_connect $2

elif [ "$Functiontype" = "cmm_status" ];then
    cmm_status $2
elif [ "$Functiontype" = "core" ];then
    pwd=`dirname  $0`
    python $pwd/ss_monitor_core.py
fi